package cn.jinjimi.app.bean;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;

import cn.jinjimi.base.util.ToastUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.controller.event.ContactsPermissionEvent;
import cn.jinjimi.app.controller.event.DataEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import static android.support.v4.content.PermissionChecker.checkSelfPermission;

/**
 * Created by lforxeverc on 16/9/13.
 */
public class Permissions {

    /*

    public static final String[] COMMON = new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.READ_LOGS,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.SET_DEBUG_APP,
            Manifest.permission.GET_ACCOUNTS,
            Manifest.permission.SYSTEM_ALERT_WINDOW
    };
    */

    public static void checkPermissions(Activity activity, int group) {
        if (Build.VERSION.SDK_INT >= 23) {

            switch (group) {
                case REQUEST_CONTACTS_PERMISSIONS: {
                    int read_contacts = checkSelfPermission(activity, Manifest.permission.READ_CONTACTS);
                    int write_contacts = checkSelfPermission(activity, Manifest.permission.WRITE_CONTACTS);

                    if (read_contacts != PackageManager.PERMISSION_GRANTED
                            || write_contacts != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.READ_CONTACTS,
                                Manifest.permission.WRITE_CONTACTS
                        }, REQUEST_CONTACTS_PERMISSIONS);
                    }

                    break;
                }

                case REQUEST_LOCATION_PERMISSIONS: {
                    int fine_location = checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
                    int coarse_location = checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION);

                    if (fine_location != PackageManager.PERMISSION_GRANTED
                            || coarse_location != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.ACCESS_FINE_LOCATION,
                                Manifest.permission.ACCESS_COARSE_LOCATION
                        }, REQUEST_LOCATION_PERMISSIONS);
                    }

                    break;
                }

                case REQUEST_STORAGE_PERMISSIONS: {
                    int read = checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
                    int write = checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

                    if (read != PackageManager.PERMISSION_GRANTED
                            || write != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE
                        }, REQUEST_MEDIA_PERMISSIONS);
                    }

                    break;
                }

                case REQUEST_MEDIA_PERMISSIONS: {

                    int microphone = checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
                    int read = checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
                    int write = checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

                    ArrayList<String> permissions = new ArrayList<>();

                    if (microphone != PackageManager.PERMISSION_GRANTED) {
                        permissions.add(Manifest.permission.RECORD_AUDIO);
                    }

                    if (read != PackageManager.PERMISSION_GRANTED
                            || write != PackageManager.PERMISSION_GRANTED) {
                        permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
                        permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    }


                    if (!permissions.isEmpty()) {
                        ActivityCompat.requestPermissions(activity, permissions.toArray(new String[permissions.size()]), REQUEST_MEDIA_PERMISSIONS);
                    }

                    break;
                }

                default:
                    break;

            }
        }
    }


    public static final int REQUEST_STORAGE_PERMISSIONS = 100;
    public static final int REQUEST_CONTACTS_PERMISSIONS = 101;
    public static final int REQUEST_LOCATION_PERMISSIONS = 102;
    public static final int REQUEST_MEDIA_PERMISSIONS = 103;


    public static void onRequestPermissionsResult(Activity activity, int requestCode, String[] permissions, int[] results) {

        switch (requestCode) {
            case REQUEST_CONTACTS_PERMISSIONS: {
                if (results.length > 0) {
                    ContactsPermissionEvent event = new ContactsPermissionEvent();

                    for (int res : results) {
                        if (res == -1) {
                            if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_CONTACTS)) {
                                ToastUtils.toastMessage(activity, R.string.need_read_contacts_permission);
                            }

                            event.state = DataEvent.FAIL;
                            EventBus.getDefault().post(event);
                            return;
                        }
                    }
                    event.state = DataEvent.SUCC;
                    EventBus.getDefault().post(event);
                }
                break;
            }
            default:break;
        }
    }
}

